﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using System.Data.SqlClient;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.SQL;

namespace Agresso.Interface.Layer.Service
{
    public class FreightCommisionInvoiceService
    {
        DataBaseConnectivity m_dataBaseConectivity;

        //public FreightCommisionInvoiceService() 
        //{
        //    m_dataBaseConectivity = new DataBaseConnectivity();
        //}

        public FreightCommisionInvoiceService(DataBaseConnectivity dataBaseConectivity)
        {
            m_dataBaseConectivity = dataBaseConectivity;
        }

        public void Execute()
        {
            try
            {
                m_dataBaseConectivity.SoftmarSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(FreightCommisionInvoiceSQL.GetAllFreightCommisionInvoice(), m_dataBaseConectivity.SoftmarSqlConnection);
                cmd.CommandTimeout = 60;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    FreightCommisionInvoice fcInvoice = new FreightCommisionInvoice(rd);
                    bool validChargeCharterer = isValidChargeCharterer(fcInvoice);
                    bool validDepartment = isValidDepartment(fcInvoice);

                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
            }
            finally
            {
                m_dataBaseConectivity.SoftmarSqlConnection.Close();
            }
        }

        private bool isValidDepartment(FreightCommisionInvoice fcInvoice)
        {
            try
            {
                int found = 0;
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(FreightCommisionInvoiceSQL.GetMiddleLayerCompanyByName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@department", System.Data.SqlDbType.VarChar).Value = fcInvoice.Department;
                LogService.WriteLogExecuteSQLCommand(cmd);
                found = Convert.ToInt16(cmd.ExecuteScalar());
                return found > 0;
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                return false;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        private bool isValidChargeCharterer(FreightCommisionInvoice fcInvoice)
        {            
            try
            {
                int found = 0;
                m_dataBaseConectivity.AgressoSqlConnection.Open();
                SqlCommand cmd = new SqlCommand("", m_dataBaseConectivity.AgressoSqlConnection);
                cmd.Parameters.Add("@apar_name", System.Data.SqlDbType.VarChar).Value = fcInvoice.ChargeCharterer;
                switch (fcInvoice.Type)
                {
                    case InvoiceType.ChargeCredit:
                        cmd.CommandText = FreightCommisionInvoiceSQL.GetAgressoSupplierCount();
                        break;
                    case InvoiceType.ChargeDebit:
                        cmd.CommandText = FreightCommisionInvoiceSQL.GetAgressoCustomerCount();
                        break;
                    case InvoiceType.CommissionCredit:
                        cmd.CommandText = FreightCommisionInvoiceSQL.GetAgressoSupplierCount();
                        break;
                    case InvoiceType.CommissionDebit:
                        cmd.CommandText = FreightCommisionInvoiceSQL.GetAgressoCustomerCount();
                        break;

                }
                LogService.WriteLogExecuteSQLCommand(cmd);
                found = Convert.ToInt16(cmd.ExecuteScalar());
                return found > 0;
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                return false;
            }
            finally
            {
                m_dataBaseConectivity.AgressoSqlConnection.Close();
            }
        }
    }
}
